package cn.jzx.scalademo1

object Demo2 extends App {

  /*
    class Graph {

      class Node {
        var connectedNodes: List[Node] = Nil

        def connectTo(node: Node) {
          if (connectedNodes.find(node.equals).isEmpty) {
            connectedNodes = node :: connectedNodes
          }
        }
      }

      var nodes: List[Node] = Nil

      def newNode: Node = {
        val res = new Node
        nodes = res :: nodes
        res
      }
    }
  */


  class Graph {

    class Node {
      var connectedNodes: List[Graph#Node] = Nil

      def connectTo(node: Graph#Node) { // Node 类中有两个this 引用 一个是this表示当前Node对象的引用, 一个是Graph.this表示是Graph的当前对象引用  改写Graph#Node
        if (connectedNodes.find(node.equals).isEmpty) {
          connectedNodes = node :: connectedNodes
        }
      }
    }

    var nodes: List[Node] = Nil

    def newNode: Node = {
      val res = new Node
      nodes = res :: nodes
      res
    }
  }


  val g: Graph = new Graph
  val n1: g.Node = g.newNode
  val n2: g.Node = g.newNode
  n1.connectTo(n2) // legal
  val h: Graph = new Graph
  val n3: h.Node = h.newNode
  n1.connectTo(n3) // illegal!




}
